package com.xinsmartsky.penmaji.penmaji;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.friendlyarm.AndroidSDK.HardwareControler;
import com.xinsmartsky.penmaji.SerialPort.FileOperation;
import com.xinsmartsky.penmaji.SerialPort.ReadSerialData;

import java.io.IOException;

public class MainActivity extends Activity implements View.OnClickListener {
    private TextView start_tv_sure;
    private PopupWindow popupWindow;
    private View start_line_root;
    private int devfd = -1;
    private String devName = "/dev/s3c2410_serial3";
    private int speed = 115200;
    private int dataBits = 8;//数据位
    private int stopBits = 1;//停止位
    private SerialPortActivity start_serialport = new SerialPortActivity();
    private EditText start_tv_password;
    private String password = "";
    private TextView start_tv_foc;

    private FileOperation fileOperation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        initPopupWindow();
        show();
        //创建文件
        operation();
        devfd = SerialPortTransStart(devName, speed, dataBits, stopBits);
    }

    private void init(){
        setContentView(R.layout.activity_main);
        start_tv_password = (EditText) findViewById(R.id.start_tv_password);
        start_tv_foc = (TextView) findViewById(R.id.start_tv_foc);
        start_tv_foc.requestFocus();//使EditText不自动弹出软键盘的方法
        start_line_root = findViewById(R.id.start_line_root);//父窗口view
        ((Button)findViewById(R.id.start_btn_sure)).setOnClickListener(this);
    }
    public void operation(){
        fileOperation = new FileOperation();
        try {
            if(fileOperation.CreateFile("serialdatablock.txt")){
                Log.i("fd", "文件创建成功");
            }else {
                Log.i("fd", "文件创建失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int SerialPortTransStart(String transdevname, int transspeed, int transdatabits, int transstopBits) {
        int fd = 0;
        fd = HardwareControler.openSerialPort(transdevname, transspeed, transdatabits, transstopBits);
        if (fd < 0) {
            Log.i("fd", "打开串口失败" + fd);
        } else {
            Log.i("fd", "打开串口成功" + fd);
            //开始读取数据
            ReadSerialData readserialdata = new ReadSerialData(fd);
            Thread t = new Thread(readserialdata);
            t.start();
        }
        return fd;
    }

    public void show() {
        start_line_root.post(new Runnable() {
            @Override
            public void run() {
                if (popupWindow != null) {
                    popupWindow.showAtLocation(start_line_root, Gravity.BOTTOM, 0, 0);
                }
            }
        });
    }

//    private String getPassword() {
//        for (int index = 0; index < 8; index++) {
//            password += Integer.toHexString(readserialdata.GetDataFromAdress(0x470 + index) & 0x000000ff);
//        }
//        return password;
//    }

    int num = 0;

    //“确定”的点击事件
    @Override
    public void onClick(View v) {
        String str = start_tv_password.getText().toString();
        if (str != null) {
            if (str.equals("1234")) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                bundle.putInt("fd", devfd);
                intent.putExtras(bundle);
                intent.setClass(MainActivity.this, LoadingActivity.class);
                startActivity(intent);
            } else {
                Toast.makeText(MainActivity.this, "密码错误！", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
        }
    }

    //初始化
    private void initPopupWindow() {
        View contentView = getLayoutInflater().inflate(R.layout.activity_main_menu, null);//null??
        RelativeLayout mstart_rela_01 = (RelativeLayout) contentView.findViewById(R.id.mstart_rela_01);
        mstart_rela_01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setAnimationStyle(R.style.popupanimation);
    }
}
